﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using DTOQLDV;

namespace DAOQLDV
{
    internal class DataAccess
    {
        private String _strcnn=SESSION._cnnStr;
        private OleDbConnection _cnn;

        public OleDbConnection Cnn
        {
            get { return _cnn; }
            set { _cnn = value; }
        }
        private OleDbTransaction _tss;
        private OleDbCommand _cmd;

        public OleDbCommand Cmd
        {
            get { return _cmd; }
            set { _cmd = value; }
        }
        public OleDbTransaction Tss
        {
            get { return _tss; }
            set { _tss = value; }
        }
        public bool Open()
        {
            bool kq = false;
            try
            {
                _cnn = new OleDbConnection(_strcnn);
                _cnn.Open();
                _cmd = _cnn.CreateCommand();
                kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
  
        public bool BeginTran()
        {
            bool kq = false;
            try
            {
                if (_cnn.State == ConnectionState.Open)
                {
                    _tss=Cnn.BeginTransaction();
                    _cmd.Transaction = _tss;
                    kq = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public bool CommitTran()
        {
            bool kq = false;
            try
            {
                if (_cnn.State == ConnectionState.Open)
                {
                    _tss.Commit();
                    kq = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public bool RollBackTran()
        {
            bool kq = false;
            try
            {
                if (_cnn.State == ConnectionState.Open)
                {
                    _tss.Rollback();
                    kq = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public bool Close()
        {
            bool kq = false;
            try
            {
                if (_cnn.State == ConnectionState.Open)
                {
                    _cnn.Close();
                    kq = true;
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public OleDbDataReader ExecuteQuery(String sql)
        {
            OleDbDataReader dr = null;
            try
            {
                _cmd = new OleDbCommand(sql, Cnn);
                dr=_cmd.ExecuteReader();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dr;
        }
        public int ExecuteNonQuery(String sql)
        {
            int kq = -1;
            try
            {
                _cmd.CommandText = sql;                
                kq=_cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public bool IsOpen()
        {
            if (Cnn.State == ConnectionState.Open)
            {
                return true;
            }
            return false;
        }
        public Object ExecuteScalar(String sql)
        {
            Object o = null;
            try
            {
                _cmd.CommandText = sql;
                o= _cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return o;
        }
    }
}
